<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容-头部-->
      <el-card class="tree-card">
        <!--放置结构内容-->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 放置一个el-tree -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容插槽内容会循环多次有多少节点就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据 data每个节点的数据对象 -->
          <TreeTools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts"/>
        </el-tree>
      </el-card>
    </div>
    <!-- 放置新增弹层组件 -->
    <AddDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Departments',
  components: {
    TreeTools: () => import('./components/tree-tools.vue'),
    AddDept: () => import('./components/add-dept.vue')
  },
  data() {
    return {
      company: {
        name: '',
        manager: ''
      }, // 就是头部的数据结构
      departs: [],
      defaultProps: {
        label: 'name', // 表示从这个属性显示内容
        children: 'children'// 从这个属性去找子节点
      },
      showDialog: false, // 控制弹层是否显示 默认不显示
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      console.log(result, 'result')
      this.company = {
        name: result.companyName,
        manager: '负责人',
        id: ''
      }
      this.departs = transListToTreeData(result.depts, '')// 需要将其转化成树形结构
    },
    // 监听tree-tools中触发的点击添加子部门的事件
    // node是我们点击的部门
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true // 弹出层
      this.node = node
      // 应该在这里调用获取部门详情的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
